package servlet;

import dao.CompanyDAOImpl;
import pojo.Company;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/company")
public class CompanyServlet extends BaseServlet {

    // 添加公司
    protected void addCompany(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//        int companyId = Integer.parseInt(req.getParameter("companyId"));
        String companyName = req.getParameter("companyName");
        String companyAddress = req.getParameter("companyAddress");

        Company company = new Company(null, companyName, companyAddress);
        CompanyDAOImpl.add(company);
        sendJsonResponse(resp, new ApiResult(200, "Company added successfully.", null));
    }

    // 删除公司
    protected void deleteCompany(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int companyId = Integer.parseInt(req.getParameter("companyId"));
        CompanyDAOImpl.delete(companyId);
        sendJsonResponse(resp, new ApiResult(200, "Company deleted successfully.", null));
    }

    // 更新公司
    protected void updateCompany(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int companyId = Integer.parseInt(req.getParameter("companyId"));
        String companyName = req.getParameter("companyName");
        String companyAddress = req.getParameter("companyAddress");

        Company company = new Company(companyId, companyName, companyAddress);
        CompanyDAOImpl.update(company);
        sendJsonResponse(resp, new ApiResult(200, "Company updated successfully.", null));
    }

    // 获取所有公司
    protected void getAllCompanies(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Company> companies = CompanyDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", companies));
    }

    // 根据公司编号获取公司
    protected void getCompanyById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int companyId = Integer.parseInt(req.getParameter("companyId"));
        Company company = CompanyDAOImpl.getById(companyId);
        sendJsonResponse(resp, new ApiResult(200, "Success", company));
    }

    // 获取公司数量
    protected void getCompanyCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = CompanyDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取公司
    protected void getCompaniesByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Company> companies = CompanyDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", companies));
    }

    // 根据关键字搜索公司
    protected void findCompaniesByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<Company> companies = CompanyDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", companies));
    }

    // 根据关键字分页搜索公司
    protected void findCompaniesByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Company> companies = CompanyDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", companies));
    }
}
